package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.service.INursingPlanService;
import com.zzyl.nursing.service.INursingProjectPlanService;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 护理计划Service业务层处理
 *
 * @author zzyl
 * @date 2025-02-21
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper,NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService iNursingProjectPlanService;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {

        NursingPlanVo nursingPlanVo = new NursingPlanVo();

        //处理业务
        //查询护理计划
        NursingPlan nursingPlan = getById(id);
        //查询护理计划相关项目
        List<NursingProjectPlan> nursingProjectPlans = iNursingProjectPlanService.list(new LambdaQueryWrapper<NursingProjectPlan>()
                .eq(NursingProjectPlan::getPlanId, id));
        //hutool拷贝方法，拷贝护理计划数据
        BeanUtil.copyProperties(nursingPlan,nursingPlanVo);
        //hutool拷贝，拷贝护理计划中的项目数据
        List<NursingProjectPlanVo> nursingProjectPlanVos = BeanUtil.copyToList(nursingProjectPlans, NursingProjectPlanVo.class);
        //封装数据
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划响应实体
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //插入护理计划相关数据
        NursingPlan nursingPlan = new NursingPlan();
        //数据封装，拷贝
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        boolean flag = save(nursingPlan);
        if (!flag) {
            throw new BaseException("保存护理计划数据失败");
        }
        //插入护理项目相关数据
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        projectPlans.forEach(v -> v.setPlanId(nursingPlan.getId()));
        flag = iNursingProjectPlanService.saveBatch(projectPlans);
        if (!flag) {
            throw new BaseException("保存护理计划项目数据失败");
        }
        return 1;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        //处理业务
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto,NursingPlan.class);
        boolean flag = updateById(nursingPlan);
        if (!flag) {
            throw new BaseException("更新护理计划数据失败");
        }
        //删除护理计划的项目的相关信息
        flag = iNursingProjectPlanService.remove(new LambdaQueryWrapper<NursingProjectPlan>()
                .eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId()));
        if (!flag) {
            throw new BaseException("删除护理计划数据失败");
        }
        //增加护理计划相关项目信息
        //插入护理项目相关数据
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        projectPlans.forEach(v -> v.setPlanId(nursingPlan.getId()));
        flag = iNursingProjectPlanService.saveBatch(projectPlans);
        if (!flag) {
            throw new BaseException("保存护理计划数据失败");
        }
        //封装数据
        return 1;
    }

    /**
     * 批量删除护理计划
     *
     * @param id 需要删除的护理计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNursingPlanByIds(Long id) {
            boolean flag = remove(new LambdaQueryWrapper<NursingPlan>().eq(NursingPlan::getId, id));
            if (!flag) {
                throw new BaseException("删除护理计划数据失败");
            }
            flag = iNursingProjectPlanService.remove(new LambdaQueryWrapper<NursingProjectPlan>().eq(NursingProjectPlan::getPlanId,id));
            if (!flag) {
                 throw new BaseException("删除护理计划的相关项目数据失败");
            }
        return 1;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询护理计划所有信息
     * @return
     */
    @Override
    public List<NursingPlan> selectNursingPlan() {
        // 查询所有
        return list();
    }

}
